import React, { Component } from "react";
import { Layout, Menu, Breadcrumb, Icon } from "antd";
import { Link,withRouter } from "react-router-dom";
import menuList from "../../config/menuConfig";
import storeutils from "../../util/storeutil"
const { Header, Content, Footer, Sider } = Layout;
const { SubMenu } = Menu;
class Leftnav extends Component {
  hasAuth=(item)=>{
    const {key,isPublic}=item;//获取item里面的key和isPublic
    const menus=storeutils.getUser().role.menus
    // console.log(11111,menus);
    const username=storeutils.getUser().username
    // 说明是超级管理员  isPublic表示所有人都具备此权限
    if(username==="admin"||isPublic||menus.indexOf(key)!==-1){
      return true
    }else if(item.children){
      return !!item.children.find((child)=>menus.indexOf(child.key)!==-1)
    }
    return false
  }
 getMenuNodes_map=(menuList)=>{
     const path=this.props.location.pathname;
  
     return menuList.map(item=>{
       if(this.hasAuth(item)){
  
         if(!item.children){
              return (
                <Menu.Item key={item.key}>
                <Link to={item.key}>
                  <Icon type={item.icon} />
                  <span>{item.title}</span>
                </Link>
              </Menu.Item>
              )
         }else{
            const cItem=item.children.find(cItem=>path.indexOf(cItem.key)===0)
            if(cItem){
                this.openKey=item.key;
            }
             return (
                <SubMenu
                key={item.key}
                title={
                  <span>
                    <Icon type={item.icon} />
                    <span>{item.title}</span>
                  </span>
                }
              >
                  {this.getMenuNodes_map(item.children)}
              </SubMenu>
             )
         }
         }
     })
 }
 componentWillMount(){
     this.menuNodes=this.getMenuNodes_map(menuList)
 }
  render() {
      const pathname=this.props.location.pathname
    //   console.log(pathname);
    const openKey=this.openKey
    return (
      <>
        <div className="logo" />
        <Menu theme="dark" defaultSelectedKeys={[pathname]} defaultOpenKeys={[openKey]} mode="inline">
        {this.menuNodes}
        </Menu>
      </>
    );
  }
}

export default withRouter(Leftnav);
